#!/bin/bash

#	curl test script of UGNM service. 
#	
#	Tests resources /users and /users/<login> including all supported HTTP operations
#	and all possible responses. Unsupported operations are not explicitly tested.


echo -e "Resource /users"
echo -e "==============="

# get all users
echo -e "\nGET - get all users\n"

curl http://localhost:8080/ugnm-service/resources/users -v -X GET

# create new user "kerry"
echo -e "\nPOST - create new user\n"

curl http://localhost:8080/ugnm-service/resources/users -v -X POST -H "Content-Type: application/json" -d "{'login':'kerry','name':'Kerry King','pass':'slayer'}"

# try to create new user "kerry" again. Should result in conflict.
echo -e "\nPOST - create new user - Conflict\n"
curl http://localhost:8080/ugnm-service/resources/users -v -X POST -H "Content-Type: application/json" -d "{'login':'kerry','name':'Kerry Kong','pass':'slushi'}"

# try to create new user with missing parameter.
echo -e "\nPOST - create new user - Missing Parameters\n"
curl http://localhost:8080/ugnm-service/resources/users -v -X POST -H "Content-Type: application/json" -d "{'login':'noparamgiver'}"

echo -e "\n\nResource /users/login"
echo -e "====================="

# get non-existing user
echo -e "\nGET - get data for non-existing user\n"
curl http://localhost:8080/ugnm-service/resources/users/idonotexist -v -X GET

# get data of user "kerry"
echo -e "\nGET - get data for existing user\n"
curl http://localhost:8080/ugnm-service/resources/users/kerry -v -X GET

# update data of user "kerry" without authorization
echo -e "\nPUT - update data for existing user - Unauthorized\n"
curl http://localhost:8080/ugnm-service/resources/users/kerry -v -X PUT -H "Content-Type: application/json" -d "{'name':'Kerry the Slayer King'}"

# update data of user "kerry"
echo -e "\nPUT - update data for existing user - no parameters\n"
curl http://localhost:8080/ugnm-service/resources/users/kerry -v -X PUT -H "Content-Type: application/json" -d "{}" --user kerry:slayer

# update data of user "kerry"
echo -e "\nPUT - update data for existing user - success\n"
curl http://localhost:8080/ugnm-service/resources/users/kerry -v -X PUT -H "Content-Type: application/json" -d "{'name':'Kerry the Slayer King'}" --user kerry:slayer

# get updated data of user "kerry"
echo -e "\nGET - get updated data for existing user\n"
curl http://localhost:8080/ugnm-service/resources/users/kerry -v -X GET

# update data of non-existing user
echo -e "\nPUT - update data for non-existing user\n"
curl http://localhost:8080/ugnm-service/resources/users/idnotexist -v -X PUT -H "Content-Type: application/json" -d "{'name':'Ido Notexist'}" --user ido:notexist

# delete user "kerry" - case Unauthorized
echo -e "\nDELETE - delete user kerry - Unauthorized\n"

curl http://localhost:8080/ugnm-service/resources/users/kerry -v -X DELETE --user kerry:wurst

# delete user "kerry" - success
echo -e "\nDELETE - delete user kerry\n"

curl http://localhost:8080/ugnm-service/resources/users/kerry -v -X DELETE --user kerry:slayer

# delete non-existing user "kerry" (was deleted before)
echo -e "\nDELETE - delete non-existing user\n"
curl http://localhost:8080/ugnm-service/resources/users/kerry -v -X DELETE --user kerry:slayer
